<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
ini_set('max_execution_time', 3600);
ini_set('memory_limit', '128M');
include_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/engine/start.php');
include_once(dirname(dirname(__FILE__)) . '/_init.php');

$limit  = 10; 
$offset = $limit * get_input('wpage');

$query = "SELECT guid FROM 3lGG_entities WHERE subtype = 13 AND owner_guid > 0 LIMIT $offset, $limit";

$results = safe_select_query($query);

print '<pre>';
echo count($results) . '<br>';
//print_r($results);

$count = 0;
foreach($results as $result) {
    $drawing = get_entity($result->guid);
    echo $drawing->guid . '<br>'; 
    if($drawing) {
        //$owner = get_user($fb->owner_guid);
        //if($owner) {
            $id                     = null;
            $title                  = null;
            $description            = null;
            $user_id                = null;
            
            $shared                 = 0;
            $width                  = 640;
            $height                 = 960;
            $orientation            = 1;
            $device_model           = null;
            $device_uid             = null;
            $lat                    = null;
            $lng                    = null;
            $location_description   = '';
            $url                    = null;
            $thumbnail_url          = null;
            $md5                    = null;
            $timestamp              = null;
            $likes                  = 0;
            
            $id                     = $drawing->guid;
            $title                  = $drawing->title;
            $description            = $drawing->description;
            if(!$description) {
                $description = $title;
            }
            $user_id                = $drawing->owner_guid;
            
            $shared                 = 0;
            $width                  = 640;
            $height                 = 960;
            $orientation            = 1;
            $device_model           = $drawing->model;
            $device_uid             = $drawing->deviceuid;
            $lat                    = $drawing->lat;
            $lng                    = $drawing->lng;
            $location_description   = '';
            $url                    = $drawing->url;
            $len = strlen($url);
            $len1 = strlen("http://www.whiteboardgallery.com/uploads/");
            $len2 = strlen(".png");
            $did = substr($url, $len1, $len - $len2 - $len1);
            if(!is_numeric($did)) {
                
            } else {
                $md5 = $did;
            }
            if($md5) {
                $cdn = file_get_contents("http://www.whiteboardgallery.com/CDN/id.php?id=$md5");
                print_r($cdn);
                $cdn = json_decode($cdn, TRUE);
                print_r($cdn);
                if($cdn && $cdn['status'] == 200) {
                    $url            = $cdn['url'];
                    $thumbnail_url  = $cdn['thumbnail_url'];  
                    $md5            = $cdn['md5'];
                } else {

                }
            }
            if($thumbnail_url) {

            } else {
                //$thumbnail_url = str_replace("rackcdn.com/", "rackcdn.com/thumb_", $url);
                $thumbnail_url = $url;
            }
            $timestamp              = $drawing->getTimeCreated();
            $likes                  = intval($drawing->likeno);
            echo "$id, $title, $description, $user_id, $device_model, $device_uid, $md5, $url, $thumbnail_url, $timestamp, $likes <br>";
           
            $nid = port_drawing($id, $title, $description, $user_id, $shared, 
                    $width, $height, $orientation, 
                    $device_model, $device_uid, 
                    $md5, $url, $thumbnail_url, 
                    $lat, $lng, $location_description, $timestamp, $likes);
            echo '<br>';
            if($nid) {
                $count++;
                $comments = $drawing->getAnnotations('generic_comment');
                foreach ($comments as $comment) {
                    add_drawing_comment($nid, $comment->owner_guid, $comment->value);
                }
            }   
            // break;
        //}
    }
}

echo $count;
?>
